<template>
    <header>
        <van-nav-bar title="搜索" left-text="返回" left-arrow @click-left="onClickLeft" />
        <van-search v-model="value" placeholder="请输入搜索关键词" @focus="show = true" @update:model-value="search(0)" @search="search(1)" />
    </header>
    <main>
        <div class="likeList" v-if="show">
            <ul>
                <li v-for="i in searchArr.arr" class="van-ellipsis" @click="search(2, i.title)">{{ i.title }}</li>
            </ul>
        </div>
        <div class="wrap" v-else>
            <EssayList class="content-wrap" @click="goTO(i.id)" v-for="i in searchArr.arr" :i="i" />
        </div>
    </main>
</template>

<script setup>
import { searchEssay } from "@/api/api";
import axios from "axios";
import { ref, reactive, defineComponent } from "vue";
import { useRouter } from "vue-router";
import EssayList from "@/components/EssayList.vue";
// 组件
defineComponent({ EssayList });
let router = useRouter();
let value = ref("");
let show = ref(false);
let searchArr = reactive({ arr: [] });
let isShow = ref(false);
let search = (s, t) => {
    console.log(s);
    if (!value.value) {
        searchArr.arr = [];
        return;
    }
    if (s) {
        show.value = false;
    }
    if (s == 2) {
        value.value = t;
    }
    axios(searchEssay(value.value)).then(({ data }) => {
        searchArr.arr = data.data;
        console.log(searchArr);
    });
};
const onClickLeft = () => router.go(-1);
let goTO = (id) => {
    router.push({ name: "articleDetails", query: { id } });
};
</script>

<style lang="scss" scoped>
main {
    background-color: #f8f8f8;
    min-height: 38.25rem;
    position: relative;
}
.likeList {
    position: absolute;
    z-index: 9999;
    top: 0;
    left: 0;
    ul {
        padding: 0 3vw 3vh;
        background-color: #fff;
        width: 100vw;
        box-sizing: border-box;
        box-shadow: 0 1px 1px rgba($color: #000000, $alpha: 0.1);
        border-radius: 0 0 5px 5px;
        li {
            width: 100%;
            height: 2.5rem;
            line-height: 2.5rem;
            border-bottom: 1px solid #f8f8f8;
        }
    }
}
.wrap {
    padding: 0 3vw 55px;
    overflow: hidden;
    .content-wrap {
        margin-top: 10px;
        border-radius: 10px;
        background-color: #fff;
        padding: 10px;
        box-sizing: border-box;
        width: 100%;
        height: 100px;
        display: flex;
    }
}
</style>
